import React,{ useReducer } from 'react'
function UseReducer(){
  const [state, dispatch] = useReducer((state, action) => {
    switch (action) {
      case 'add':
        state+=1
        break;
      case 'sub':
        if(state>=1){
          state-=1
        }
        break;
      default:
        break;
    }
    return state
  },0)
  return (
    <div>
      点击了{state}次<br /><br />
      <button onClick={()=>dispatch('add')}>加1</button>
      <button onClick={()=>dispatch('sub')}>减1</button>

    </div>
  )
  
}

export default UseReducer